#pragma once
#include <sys/epoll.h>
class Epoll
{
    const static int gnum = 128;
    const static int gtimeout = 5000;
public:
    Epoll(int timeout = gtimeout) : _timeout(timeout)
    {}
    void CreatEpoll()
    {
        _epfd = epoll_create(gnum);
        if(_epfd < 0) exit(5);
    }
    bool AddSockToEpoll(int sock,uint32_t event)
    {
        struct epoll_event ev;
        ev.data.fd = sock;
        ev.events = event;
        int n = epoll_ctl(_epfd,EPOLL_CTL_ADD,sock,&ev);
        return n == 0;
    }
    int WaitPoll(struct epoll_event revs[],int revs_nums)
    {

        return epoll_wait(_epfd,revs,revs_nums,_timeout);
    }
    bool CtlPoll(int sock,uint32_t event)
    {
        event |= EPOLLET;
        struct epoll_event ev;
        ev.data.fd = sock;
        ev.events = event;
        int n = epoll_ctl(_epfd,EPOLL_CTL_MOD,sock,&ev);
        return n == 0;
    }
    bool DelFromEpoll(int sock)
    {
        int n = epoll_ctl(_epfd,EPOLL_CTL_DEL,sock,nullptr);
        return n == 0;
    }
    ~Epoll()
    {}

private:
    int _epfd;
    int _timeout;
};